package com.lenovo.leos.filebrowser.netdisk;

import java.util.Timer;
import java.util.TimerTask;

import com.lenovo.leos.filebrowser.netdisk.NetDiskOpService.IServiceBinder;

//import android.R;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

import com.lenovo.leos.filebrowser.netdisk.Log;

import com.lenovo.leos.filebrowser.netdisk.NetDiskConstant;
import com.lenovo.leos.filebrowser.netdisk.NetFile;
import com.lenovo.leos.letools.lenovodata.DownLoadServer;
import com.lenovo.leos.letools.lenovodata.UploadServer;

/*
 * add a listener
 * add getOpStatus function
 * add an intent message
 * add the function
 * isopfinish()
 * getopResult();
 * [maybe] cleanup()
 */

public class NetDiskManager {
	private static final String TAG = "NetDiskManager";

	public static final int MSG_CONNECT_SERVICE_OK = 1000000;
	public static final int MSG_CONNECT_SERVICE_FAILED = 1000001;
	public static final String NETDISK_OP_SERVICE_NAME = "com.lenovo.leos.filebrowser.netdisk.NetDiskOpService";

	private static final String ND_ROOT_ID = "1300933610116208337";

	
	
	public interface OnNotifyListener {
		public void onOpNotify(Long opId, Bundle data);

		public void onServiceNotify(int msgId, String content);
	}

	// use to handle asynch issue
	private class CheckTimerTask extends TimerTask {

		// true => check bind status
		// false => check unbind status
		private boolean mbToBind = true;

		public CheckTimerTask(boolean bindOrUnbind) {
			mbToBind = bindOrUnbind;
		}

		@Override
		public void run() {
			Log.i(TAG, "==> run of CheckTimerTask");

			if (null != mService) {
				Log.i(TAG, "just got service binder");

				if (cancel()) {
					Log.i(TAG, "cancel the CheckTimerTask");

					if (mbToBind) {
						// maybe call listener to notify
						if (null != mListener)
							mListener.onServiceNotify(MSG_CONNECT_SERVICE_OK,
									null);

						// initialize the service
						mService.setEnableNotify(mbEnableNotify);
					} else {
						mContext.unbindService(serviceConnection);
					}
				} else {

				}
			} else {
				Log.i(TAG, "==> run of CheckTimerTask is NULL");
			}
		}
	}

	private ServiceConnection serviceConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.i(TAG, "==> onServiceConnected");

			mService = (IServiceBinder) service;
		}

		public void onServiceDisconnected(ComponentName name) {
			Log.i(TAG, "==> onServiceDisconnected");
			mService = null;
		}
	};

	private static NetDiskManager sInstance = null;

	private static Context mContext = null;
	private static NetDiskOpService.IServiceBinder mService = null;
	private BroadcastReceiver mReceiver = null;
	private IntentFilter mFilter = null;
	private OnNotifyListener mListener = null;

	private boolean mbCallBind = false;
	private boolean mbEnableNotify = false;

	private NetDiskManager(Context context) {
		Log.i(TAG, "==> NetDiskManager");

		mContext = context;
		mbCallBind = false;
		mService = null;
	}

	static DownLoadServer mmdlserver = DownLoadServer.getInstance(mContext);
	static UploadServer mulServer = UploadServer.getInstance(mContext);
	
	public synchronized static NetDiskManager getInstance(Context context) {
		Log.i(TAG, "==> getInstance");

		if (null == context) {
			Log.e(TAG, "context is null in getInstance");
			return null;
		}

		if (null == sInstance) {
			sInstance = new NetDiskManager(context);
		} else {
			sInstance.mContext = context;
			sInstance.mbCallBind = false;
			sInstance.mService = null;
		}

		// start op service at first to make sure the service will not stop
		// when the activity is finished
		startOpService();

		return sInstance;
	}

	private static void startOpService() {
		Log.i(TAG, "==> startOpService");

		sInstance.mContext.startService(new Intent(NETDISK_OP_SERVICE_NAME));
	}

	public boolean bindOpService() {
		Log.i(TAG, "==> bindOpService");

		if (null != mService) {
			Log.i(TAG, "had been bound in bindOpService");
			return true;
		} else {
			// create receiver if necessory
			if (mbEnableNotify) {
				registerReceiver();
			}

			if (mbCallBind) {
				Log.i(TAG, "it in binding in bindOpService");
				return false;
			} else {
				mContext.bindService(new Intent(NETDISK_OP_SERVICE_NAME),
						serviceConnection, Context.BIND_AUTO_CREATE);
				mbCallBind = true;
				Log.i(TAG, "bind service");
				if (null == mService) {
					// start timer to check every interval
					Log.i(TAG, "service is null");
					Timer timer = new Timer();
					timer.schedule(new CheckTimerTask(true), 200, 200);
					return false;
				} else
					return true;
			}
		}
	}

	private void registerReceiver() {
		Log.i(TAG, "==> registerReceiver");

		if (null == mReceiver) {
			mReceiver = new BroadcastReceiver() {
				@Override
				public void onReceive(Context context, Intent intent) {
					Log.i(TAG, "==> onReceive");

					if (null != mListener && null != intent) {
						long id = intent.getLongExtra(
								NetDiskConstant.OP_DATA_KEY_ID, -1);
						mListener.onOpNotify(id, intent.getExtras());
					}

				}
			};
		}

		if (null == mFilter) {
			mFilter = new IntentFilter();
			mFilter.addAction(NetDiskConstant.ACTION_NOTIFY_RESULT);
		}

		if (null != mReceiver && null != mFilter) {
			Log.i(TAG, "register receiver");
			mContext.registerReceiver(mReceiver, mFilter);
		} else {
			Log.e(TAG, "can not register receiver");
		}
	}

	// [improve] more code to handle asynchronous issue
	private void unregisterReceiver() {
		Log.i(TAG, "==> unregisterReceiver");

		if (null != mReceiver) {
			try {
				mContext.unregisterReceiver(mReceiver);
			} catch (Exception e) {
				// if failed, unregister in timer;
				return;
			}

			mReceiver = null;
		}
	}

	public boolean unbindOpService() {
		Log.i(TAG, "==> unbindOpService");

		if (!mbCallBind)
			return true;
		else {
			// unregister receiver
			if (mbEnableNotify && null != mReceiver) {
				unregisterReceiver();

			}

			if (null != mService) {
				mContext.unbindService(serviceConnection);
				return true;
			} else {
				// start timer to check every interval
				Timer timer = new Timer();
				timer.schedule(new CheckTimerTask(false), 200, 200);
				return false;
			}
		}
	}

	public void setOnOpNotifyListener(OnNotifyListener listener) {
		mListener = listener;
	}

	public boolean isConnected() {
		if (null != mService)
			return true;
		else
			return false;
	}

	public long login(String userName, String pwd) {
		if (null == userName || 0 == userName.trim().length() || null == pwd
				|| 0 == pwd.trim().length()) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {
			if (null != mService) {
				Bundle bundle = new Bundle();
				bundle.putString("user_name", userName);
				bundle.putString("password", pwd);
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.login, bundle);

				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;
			}
		}
	}
	
	public long getSpace() {
		if (null != mService) {
			Bundle bundle = new Bundle();
			SimpleNetDiskOp sop = new SimpleNetDiskOp(
					SimpleNetDiskOp.Type.get_space, bundle);
			mService.addOp(sop);
			return sop.getID();
		} else {
			return NetDiskOp.INVALID_ID;
		}
	}

	public long getDirContent(String dirpath) {
		if (null == dirpath || 0 == dirpath.trim().length()) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {
			if (null != mService) {
				Bundle bundle = new Bundle();

				// [improve] get it from cache
				if (dirpath.equals("/"))
					dirpath = ND_ROOT_ID;
				bundle.putString("dirpath", dirpath);
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.get_dir_content, bundle);
				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;
			}
		}
	}

	public long getDirContent(NetFile nf) {
		if (null == nf) {
			Log.e(TAG, "nf is null in getDirContent");
			return NetDiskOp.INVALID_ID;
		} else {
			if (!nf.isDirectory()) {
				Log.e(TAG, "nf is not a dir");
				return NetDiskOp.INVALID_ID;
			}

			if (null != mService) {
				Bundle bundle = new Bundle();

				// [improve] get it from cache
				// if (dirpath.equals("/"))
				// dirpath = ND_ROOT_ID;

				bundle.putString("dirpath", nf.getAbsolutePath());
				bundle.putString("dirid", nf.getID());
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.get_dir_content, bundle);
				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;
			}
		}
	}

	public long getSubDirInDir(String dirpath) {
		if (null == dirpath || 0 == dirpath.trim().length()) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {
			if (null != mService) {
				Bundle bundle = new Bundle();
				bundle.putString("dirpath", dirpath);
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.list_subdir_in_dir, bundle);
				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;
			}
		}
	}

	public long getFilesInDir(String dirpath) {
		if (null == dirpath || 0 == dirpath.trim().length()) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {
			if (null != mService) {
				Bundle bundle = new Bundle();
				bundle.putString("dirpath", dirpath);
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.list_files_in_dir, bundle);
				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;
			}
		}   
	}
   
	public long createDir(String parent, String name) {
		if (null == parent || 0 == parent.trim().length() || null == name
				|| 0 == name.trim().length()) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {
			if (null != mService) {  
				Bundle bundle = new Bundle();
				bundle.putString("parent", parent);
				bundle.putString("name", name);
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.create_dir, bundle);

				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;  
			}
		}
	}

	public long createDir(NetFile netfileitem, String name) {
		if (null == netfileitem || null == name || 0 == name.trim().length()) {
			Log.e(TAG, "invalid input in createDir");
			return NetDiskOp.INVALID_ID;
		} else {
			if (null != mService) {
				Bundle bundle = new Bundle();
				bundle.putString("parentid", netfileitem.getID());
				bundle.putString("parent", netfileitem.getAbsolutePath());
				bundle.putString("name", name);
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.create_dir, bundle);

				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;
			}
		}
	}

	public long deleteDir(String dirpath) {
		if (null == dirpath || 0 == dirpath.trim().length()) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {
			if (null != mService) {
				Bundle bundle = new Bundle();
				bundle.putString("dirpath", dirpath);
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.delete_dir, bundle);
				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;
			}
		}
	}

	public long deleteDir(NetFile netfileitem) {
		if (netfileitem == null) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {
			if (null != mService) {
				Bundle bundle = new Bundle();
				bundle.putString("dirpath", netfileitem.getID());
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.delete_dir, bundle);
				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;
			}
		}
	}

	public long renameDir(String dirpath, String newname) {
		if (null == dirpath || 0 == dirpath.trim().length() || null == newname
				|| 0 == newname.trim().length()) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {
			if (null != mService) {
				Bundle bundle = new Bundle();
				bundle.putString("dirpath", dirpath);
				bundle.putString("newname", newname);
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.rename_dir, bundle);

				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;
			}
		}
	}

	public long renameDir(NetFile netfileitem, String newName) {
		if (null == netfileitem) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {
			// if (netfileitem.hasNewName()) {
			if (null != mService) {
				Bundle bundle = new Bundle();
				// bundle.putString("dirpath", dirpath);
				// bundle.putString("newname", newname);
				bundle.putString("dirpath", netfileitem.getID());
				bundle.putString("newname", newName);
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.rename_dir, bundle);

				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;
			}

			// } else {
			// return NetDiskOp.INVALID_ID;
			// }
		}
	}

	// public long renameDirorFile(String strNameID, String newName) {
	// if (null == strNameID || strNameID.trim().length() == 0
	// || null == newName || newName.trim().length() == 0) {
	// Log.e(TAG, "invalid input in login");
	// return NetDiskOp.INVALID_ID;
	// } else {
	//
	// if (null != mService) {
	// if (netfileitem.isDirectory()) {
	// Bundle bundle = new Bundle();
	// // bundle.putString("dirpath", dirpath);
	// // bundle.putString("newname", newname);
	// bundle.putString("dirpath", netfileitem.getID());
	// bundle.putString("newname", netfileitem.getNewName());
	// SimpleNetDiskOp sop = new SimpleNetDiskOp(
	// SimpleNetDiskOp.Type.rename_dir, bundle);
	//
	// mService.addOp(sop);
	// return sop.getID();
	// } else {netfileitem
	// Bundle bundle = new Bundle();
	// // bundle.putString("dirpath", dirpath);
	// // bundle.putString("newname", newname);
	// bundle.putString("filepath", netfileitem.getID());
	// bundle.putString("newname", netfileitem.getNewName());
	// SimpleNetDiskOp sop = new SimpleNetDiskOp(
	// SimpleNetDiskOp.Type.rename_file, bundle);
	// mService.addOp(sop);
	// return sop.getID();
	// }
	//
	// } else {
	// return NetDiskOp.INVALID_ID;
	// }
	// }
	// }

	public long renameDirorFile(NetFile netfileitem, String newName) {
		if (null == netfileitem || null == newName
				|| 0 == newName.trim().length()) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {

			// if (netfileitem.hasNewName()) {
			if (null != mService) {
				if (netfileitem.isDirectory()) {

					Bundle bundle = new Bundle();
					// bundle.putString("dirpath", dirpath);
					// bundle.putString("newname", newname);
					bundle.putString("dirname", netfileitem.getName());
					bundle.putString("parent", netfileitem.getParent());
					bundle.putString("type", "dir");

					bundle.putString("dirpath", netfileitem.getID());
					bundle.putString("newname", newName);
					SimpleNetDiskOp sop = new SimpleNetDiskOp(
					SimpleNetDiskOp.Type.rename_dir, bundle);
       
					mService.addOp(sop);
					return sop.getID();
				} else {
					Bundle bundle = new Bundle();
					// bundle.putString("dirpath", dirpath);
					// bundle.putString("newname", newname);
					bundle.putString("filename", netfileitem.getName());
					bundle.putString("parent", netfileitem.getParent());
					bundle.putString("type", "file");

					bundle.putString("filepath", netfileitem.getID());
					bundle.putString("newname", newName);
					SimpleNetDiskOp sop = new SimpleNetDiskOp(
							SimpleNetDiskOp.Type.rename_file, bundle);
					mService.addOp(sop);
					return sop.getID();
				}
			} else {
				return NetDiskOp.INVALID_ID;
			}

			// } else {
			// return NetDiskOp.INVALID_ID;
			// }
		}
	}

	public long deleteFile(String filepath) {
		if (null == filepath || 0 == filepath.trim().length()) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {
			if (null != mService) {
				Bundle bundle = new Bundle();
				bundle.putString("filepath", filepath);
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.delete_file, bundle);
				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;
			}
		}
	}

	public long deleteFileorDir(NetFile netfileitem) {
		if (null == netfileitem) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {
			if (null != mService) {
				if (netfileitem.isFile()) {
					Bundle bundle = new Bundle();
					bundle.putString("filepath", netfileitem.getID());
					SimpleNetDiskOp sop = new SimpleNetDiskOp(
							SimpleNetDiskOp.Type.delete_file, bundle);
					mService.addOp(sop);
					return sop.getID();
				} else {
					Bundle bundle = new Bundle();
					bundle.putString("dirpath", netfileitem.getID());
					SimpleNetDiskOp sop = new SimpleNetDiskOp(
							SimpleNetDiskOp.Type.delete_dir, bundle);
					mService.addOp(sop);
					return sop.getID();
				}
			} else {
				return NetDiskOp.INVALID_ID;
			}
		}
	}

	public long renameFile(String filepath, String newname) {
		if (null == filepath || 0 == filepath.trim().length()
				|| null == newname || 0 == newname.trim().length()) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {
			if (null != mService) {
				Bundle bundle = new Bundle();
				bundle.putString("filepath", filepath);
				bundle.putString("newname", newname);
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.rename_file, bundle);

				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;
			}
		}
	}

	public long moveFile(String filepath, String newpath, String override) {
		if (null == filepath || 0 == filepath.trim().length()
				|| null == newpath || 0 == newpath.trim().length()
				|| null == override || 0 == override.trim().length()) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {
			if (null != mService) {
				Bundle bundle = new Bundle();
				bundle.putString("filepath", filepath);
				bundle.putString("newpath", newpath);
				bundle.putString("override", override);
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.move_file, bundle);

				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;
			}
		}
	}

	public long moveDir(String filepath, String newpath) {
		if (null == filepath || 0 == filepath.trim().length()
				|| null == newpath || 0 == newpath.trim().length()) {
			Log.e(TAG, "invalid input in login");
			return NetDiskOp.INVALID_ID;
		} else {
			if (null != mService) {
				Bundle bundle = new Bundle();
				bundle.putString("filepath", filepath);
				bundle.putString("newpath", newpath);
				SimpleNetDiskOp sop = new SimpleNetDiskOp(
						SimpleNetDiskOp.Type.move_dir, bundle);

				mService.addOp(sop);
				return sop.getID();
			} else {
				return NetDiskOp.INVALID_ID;
			}
		}
	}
	 
//	public long downloadFile(String fileid, String strFileName,String strDownLoadPath) {
//		if (null == fileid || 0 == fileid.trim().length()) {
//			Log.e(TAG, "invalid input in login");
//			return NetDiskOp.INVALID_ID;
//		} else {
//			if (null != mService) {
//				Bundle bundle = new Bundle();
//				Log.i(TAG, "==>downloadFile"+strFileName);
//				bundle.putString("downLoadPath", strDownLoadPath);
//				bundle.putString("filename", strFileName);
//				bundle.putString("fileid", fileid);
//				LoadNetDiskOp sop = new LoadNetDiskOp(
//				LoadNetDiskOp.Type.download_file, bundle);
//				sop.setState(LoadNetDiskOp.State.ready);
//				mService.addOp(sop);
//				return sop.getID();
//			} else {
//				return LoadNetDiskOp.INVALID_ID;
//			}
//		}
//	}
//	public long[] downloadFile(String[] fileid, String[] strFileName,String strDownLoadPath) {
//		long [] opIdList = null;
//		for(int i=0;i<fileid.length;i++){
//			opIdList[i] = downloadFile(fileid[i], strFileName[i],strDownLoadPath);
//			if(opIdList[i] == LoadNetDiskOp.INVALID_ID){
//				return null; 
//			}
//		}
//		return opIdList;
//	}
//	public long downLoadDir(String strdirid,String strDirName,String strDownLoadPath){
//		if (null == strdirid || 0 == strdirid.trim().length()||null == strDirName || 0 == strDirName.trim().length()
//				||null == strDownLoadPath || 0 == strDownLoadPath.trim().length()) {
//			Log.e(TAG, "invalid input in login");
//			return NetDiskOp.INVALID_ID;
//		} else {
//			if (null != mService) {
//				Bundle bundle = new Bundle();
//				Log.i(TAG, "==>downloadDor"+strDirName);
//				bundle.putString("dirid", strdirid);
//				bundle.putString("dirname", strDirName);
//				bundle.putString("downLoadPath", strDownLoadPath);
//				LoadNetDiskOp sop = new LoadNetDiskOp(
//				LoadNetDiskOp.Type.download_dir, bundle);
//				sop.setState(LoadNetDiskOp.State.ready);
//				mService.addOp(sop);
//				return sop.getID();
//			} else {
//				return LoadNetDiskOp.INVALID_ID;
//			}
//		}
//	}
//	public long uploadFile(String strDirId, String strPath) {
//		if (null == strDirId || 0 == strDirId.trim().length()
//				|| null == strPath || 0 == strPath.trim().length()) {
//			Log.e(TAG, "invalid input in login");
//			return NetDiskOp.INVALID_ID;
//		} else {
//			if (null != mService) {
//				Log.i(TAG,"do uploadFile");
//				Bundle bundle = new Bundle();
//				bundle.putString("DirId", strDirId);
//				bundle.putString("Path", strPath);
//				LoadNetDiskOp sop = new LoadNetDiskOp(
//						LoadNetDiskOp.Type.upload_file, bundle);
//				sop.setState(LoadNetDiskOp.State.ready);
//				mService.addOp(sop);
//				return sop.getID();
//			} else {
//				return NetDiskOp.INVALID_ID;
//			}
//		}
//	}
//	public long[] uploadFileList(String strDirId, String[] strPath) {
//		long [] opIdList = null;
//		for(int i=0;i<strPath.length;i++){
//			opIdList[i] = uploadFile(strDirId, strPath[i]);
//			if(opIdList[i] == LoadNetDiskOp.INVALID_ID){
//				return null; 
//			}
//		}
//		return opIdList;
//	}
//	public long upLoadDir(String strparentDirid,String strdirWholepath){
//		if (null == strparentDirid || 0 == strparentDirid.trim().length()||null == strdirWholepath || 0 == strdirWholepath.trim().length()
//				) {
//			Log.e(TAG, "invalid input in login");
//			return NetDiskOp.INVALID_ID;
//		} else {
//			if (null != mService) {
//				Bundle bundle = new Bundle();
//				Log.i(TAG, "==>downloadDor"+strdirWholepath);
//				bundle.putString("parentDirId", strparentDirid);
//				bundle.putString("dirWholepath", strdirWholepath);
//				LoadNetDiskOp sop = new LoadNetDiskOp(
//				LoadNetDiskOp.Type.upload_dir, bundle);
//				sop.setState(LoadNetDiskOp.State.ready);
//				mService.addOp(sop);
//				return sop.getID();
//			} else {
//				return LoadNetDiskOp.INVALID_ID;
//			}
//		}
//	}
	
	// public long downloadDir(String filepath, String savepath) {
	// return -1;
	// }
	//
	// public long uploadDir(String localfile, String netpath){
	// return -1;
	// }
//	public long pauseLoad(LoadedData netLoadData) {
//		if (null == netLoadData) {
//			Log.e(TAG, "invalid input in login");
//			return NetDiskOp.INVALID_ID;
//		} else {
//			if (null != mService) {
//				Bundle bundle = new Bundle();
//				Log.i(TAG, "==>pauseLoad");
//				bundle.putLong("OpID", netLoadData.getmOpID());
//				LoadNetDiskOp sop = new LoadNetDiskOp(
//						LoadNetDiskOp.Type.do_pause, bundle);
//				sop.setState(LoadNetDiskOp.State.ready);
//				mService.addOp(sop);
//				return sop.getID();
//			} else {
//				return LoadNetDiskOp.INVALID_ID;
//			}
//		}
//
//	}  

//	public long resumeLoad(LoadedData netLoadData) {
//
//		if (null == netLoadData) {
//			Log.e(TAG, "invalid input in login");
//			return NetDiskOp.INVALID_ID;
//		} else {
//			if (null != mService) {
//				Bundle bundle = new Bundle();
//				Log.i(TAG, "==>pauseLoad");
//				bundle.putLong("OpID", netLoadData.getmOpID());
//				LoadNetDiskOp sop = new LoadNetDiskOp(
//						LoadNetDiskOp.Type.do_start, bundle);
//				sop.setState(LoadNetDiskOp.State.ready);
//				mService.addOp(sop);
//				return sop.getID();
//			} else {
//				return LoadNetDiskOp.INVALID_ID;
//			}
//		}
//	}

//	public long cancelLoad(LoadedData netLoadData) {
//		if (null == netLoadData) {
//			return NetDiskOp.INVALID_ID;
//		} else {  
//			if (null != mService) {
//				Bundle bundle = new Bundle();
//				Log.i(TAG, "==>pauseLoad");
//				bundle.putLong("OpID", netLoadData.getmOpID());
//				LoadNetDiskOp sop = new LoadNetDiskOp(
//						LoadNetDiskOp.Type.do_clean, bundle);
//				sop.setState(LoadNetDiskOp.State.ready);
//				mService.addOp(sop);
//				return sop.getID();
//			} else {
//				return LoadNetDiskOp.INVALID_ID;
//			}
//		}
//	}
//	public void celSleAllLoad(){
//		Bundle bundle = new Bundle();
//		LoadNetDiskOp sop = new LoadNetDiskOp(
//		LoadNetDiskOp.Type.do_clean_all, bundle);
//		sop.setState(LoadNetDiskOp.State.ready);
//		mService.addOp(sop);
//	}
	public long stopLoad(LoadedData netLoadData) {
		return -1;
	}

	public long pauseLoad(long opId) {
		return -1;
	}

	public long resumeLoad(long opId) {
		return -1;
	}

	public long stopLoad(long opId) {
		return -1;
	}

	// below functions are used to get op status
	public boolean isFinished(long opId) {
		return true;
	}
	public boolean IsRuning(String strWholePath,boolean isFile){
		
		
		return true;
	}

	public synchronized NetDiskOpResult getResult(long opId) {
		Log.i(TAG, "==> getResult");
		if (0 > opId) {
			Log.e(TAG, "opid is " + opId);
			Log.e(TAG, "invalid id in getResult");
			return null;
		} else {
			if (null != mService) {
				String result = mService.getResult(opId);
				if (null == result || 0 == result.trim().length()) {
					return null;
				} else
					return new NetDiskOpResult(result);
			} else {
				Log.e(TAG, "not connected to service");
				return null;
			}
		}
	}  
  
//	public ArrayList<LoadedData> getProgess(long opId) {
//		Log.i(TAG, "==> getProgess"+opId);
//		if (0 > opId) {
//			Log.e(TAG, "opid is " + opId);
//			Log.e(TAG, "invalid id in getResult");
//			return null;
//		} else {
//			if (null != mService) {
//				Log.i(TAG, "==>go to service getProgess");
//				ArrayList<LoadedData> retList = mService.getProgess(opId);
//				if(retList != null){
//					Log.i(TAG, "*******retList" + retList + "list>>>>>>>>>>"
//							+ retList.size());
//				}
//				return retList;
//				      
//				// return mService.getProgess(opId);
//			} else {
//				return null;
//			}
//		}
//	}  

	public void setEnableNotify(boolean enable) {
		Log.i(TAG, "==> setEnableNotify");

		if (mbEnableNotify != enable) {
			mbEnableNotify = enable;

			if (mbEnableNotify) {
				registerReceiver();
			} else {
				unregisterReceiver();
			}

			if (null != mService) {
				mService.setEnableNotify(mbEnableNotify);
			} else {
				// set in timer
			}

		}
	}
	
//	public static  boolean isDownLoadWork(String wholePath){
//		Log.d(TAG, "the wholePath is ============ " + wholePath);
//		Log.d(TAG, "the mmdlserver is ============ " + mmdlserver);
//		Log.d(TAG, "the result is ============ " + mmdlserver.IsDownLoading(wholePath));
//		return mService.IsDownLoading(wholePath);
		//return mmdlserver.IsDownLoading(wholePath);
		//		List<FileDownloadEntity> filelist = mmdlserver.getFileLoadTask();
//		for(int i=0;i<filelist.size();i++){
//			//one download file
//			String strDownLoadPath = filelist.get(i).getFileWholePath();
//			if(wholePath.equals(strDownLoadPath)){
//				
//				return true;
//			}
//		}
//		
//		Map<String, List<FileDownloadEntity>> maplist = mmdlserver.getDirLoadTask();
//		Set<Map.Entry<String, List<FileDownloadEntity>>> TempValue =  maplist.entrySet();
//		for(Map.Entry<String, List<FileDownloadEntity>> entry : TempValue){
//			List<FileDownloadEntity> templist = entry.getValue();
//			for(int i=0;i<templist.size();i++){
//				//one downlaod dir task
//				if(wholePath.equals(templist.get(i).getFileWholePath())){
//					return true;
//				}
//			}
//		}
//		return false;
//	}
//	public static  boolean isUpLoadWork(String wholePath){
//		//return mulServer.IsUpLoad(wholePath);
//		return mService.IsUpLoading(wholePath);
//	}
//	public String NotificationString(){
//		
//		String strRetString;
//		ArrayList<LoadedData> tasklist = getProgess(31415);
//		int downruntotalnum = 0,uploadtotalnum = 0,
//		downloadpausenum = 0, uploadpausenum = 0,
//		downloadfinishnum = 0,uploadfinishnum = 0,
//		downloaderrornum = 0,uploaderrornum = 0,
//		downloadwaitnum = 0,uploadwaitnum = 0;
//		
//		for(int i=0;i<tasklist.size();i++){
//			if(tasklist.get(i).IsDownLoad()){
//				if(tasklist.get(i).getState() == LoadedData.tag_state.run){
//					downruntotalnum++;
//				}
//				if(tasklist.get(i).getState() == LoadedData.tag_state.pause){
//					downloadpausenum++;
//				}
//				if(tasklist.get(i).getState() == LoadedData.tag_state.finish){
//					downloadfinishnum++;
//				}
//				if(tasklist.get(i).getState() == LoadedData.tag_state.error){
//					downloaderrornum++;
//				}
//				if(tasklist.get(i).getState() == LoadedData.tag_state.ready){
//					downloadwaitnum++;
//				}
//			}else{
//				if(tasklist.get(i).getState() == LoadedData.tag_state.run){
//					uploadtotalnum++;
//				}
//				if(tasklist.get(i).getState() == LoadedData.tag_state.pause){
//					uploadpausenum++;
//				}
//				if(tasklist.get(i).getState() == LoadedData.tag_state.finish){
//					uploadfinishnum++;
//				}
//				if(tasklist.get(i).getState() == LoadedData.tag_state.error){
//					uploaderrornum++;
//				}
//				if(tasklist.get(i).getState() == LoadedData.tag_state.ready){
//					uploadwaitnum++;
//				}
//			}
//		}
//		strRetString  = mContext.getString(R.string.upload_task);
//		strRetString =strRetString+ "\n";
//		strRetString =strRetString+uploadtotalnum+ mContext.getString(R.string.upload_going);
//		strRetString =strRetString+uploadpausenum+ mContext.getString(R.string.pause_number);
//		strRetString =strRetString+uploadfinishnum+ mContext.getString(R.string.done_number);
//		strRetString =strRetString+uploaderrornum+ mContext.getString(R.string.failure_number);
//		strRetString =strRetString+uploadwaitnum+ mContext.getString(R.string.waittask);
//		strRetString =strRetString+ "\n";    
//		strRetString  = strRetString+mContext.getString(R.string.download_task);
//		strRetString =strRetString+ "\n";
//		strRetString =strRetString+downruntotalnum+ mContext.getString(R.string.going_number);
//		strRetString =strRetString+downloadpausenum+ mContext.getString(R.string.pause_number);
//		strRetString =strRetString+downloadfinishnum+ mContext.getString(R.string.done_number);
//		strRetString =strRetString+downloaderrornum+ mContext.getString(R.string.failure_number);
//		strRetString =strRetString+downloadwaitnum+ mContext.getString(R.string.waittask);
//		  
//		return strRetString;
//	}
}